//
//  YYTabBar.swift
//  新浪微博
//
//  Created by fdq on 16/7/19.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

protocol YYTabBarDelegate : NSObjectProtocol {
	
	func tabBarDelegateMethod()
}



class YYTabBar: UITabBar {
	
	weak var yyDelegate : YYTabBarDelegate?

	override init(frame: CGRect) {
		super.init(frame: frame)
		setupUI()
	}

	required init?(coder aDecoder: NSCoder) {
	    fatalError("init(coder:) has not been implemented")
	}
	
	func setupUI(){
		
		addSubview(addButton)
		
		addButton.snp_makeConstraints { (make) -> Void in
			make.center.equalTo(self.center)
		}
		
	}
	
	//按钮监听方法
	func didClickAddButton(){
		
		yyDelegate?.tabBarDelegateMethod()
		
	}
	
	override func layoutSubviews() {
		super.layoutSubviews()
		let width : CGFloat = self.frame.size.width * 0.2
		var index : CGFloat = 0
		
		
		for subView in subviews{
			if subView.isKindOfClass(NSClassFromString("UITabBarButton")!){

				subView.frame.origin.x = width * index
				index++
				
				if index == 2{
					index++
				}
			}
		}
	}
	
	//懒加载
	lazy var addButton : UIButton = {
	
		let button = UIButton()
		button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
		button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
		button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
		button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
		button.sizeToFit()
		
		button .addTarget(self, action: "didClickAddButton", forControlEvents: UIControlEvents.TouchUpInside)
		return button
	}()

}
